package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Orderbill;
import cn.wolfcode.wms.domain.Supplier;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.OrderbillQueryObject;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.OrderbillService;
import cn.wolfcode.wms.service.SupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orderbill")
public class OrderbillController {
    @Autowired
    private OrderbillService orderbillService;
    @Autowired
    private SupplierService supplierService;
    @RequiredPermission("订单查询")
    @RequestMapping("/list")
    public String list(Map map, OrderbillQueryObject qo) {
        PageResult result = orderbillService.query(qo);
        map.put("qo",qo);
        map.put("supplier",supplierService.selectAll());
        map.put("result",result);
        return "orderbill/list";
    }
    @RequiredPermission("订单编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {
        List<Supplier> suppliers = supplierService.selectAll();
            map.put("supplier",suppliers);
        if (id != null && id != 0) {
            Orderbill orderbill = orderbillService.getById(id);
            System.out.println(orderbill.getItems());
            map.put("orderbill", orderbill);
        }
        return "/orderbill/input";
    }
    @RequiredPermission("订单删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        orderbillService.deleteById(id);
        try {

        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return  JSONResult.success("删除成功");
    }
    @RequiredPermission("订单新增/修改")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Orderbill orderbill) {

        try {
            orderbillService.updateOrSave(orderbill);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("新增/修改成功");
    }
    @RequiredPermission("订单审核")
    @RequestMapping("/audit")
    @ResponseBody
    public JSONResult audit(Long id){
        try {

            orderbillService.audit(id);
        }catch (Exception e){
            e.printStackTrace();
            JSONResult.mark("e");
        }
        return JSONResult.success("审核成功");
    }
    @RequestMapping("/views")
    public String views(Map map, Long id) {
        List<Supplier> suppliers = supplierService.selectAll();
        map.put("supplier",suppliers);
        if (id != null && id != 0) {
            Orderbill orderbill = orderbillService.getById(id);
            System.out.println(orderbill.getItems());
            map.put("orderbill", orderbill);
        }
        return "/orderbill/views";
    }
}
